#include "Mcs.h"

Mcs::~Mcs()
{
    for(McsFrame* fp : frames)
        delete fp;
}

bool Mcs::TryLoad(std::istream &i)
{
    char check[7];
    i.read(check, 6);
    if(std::string(check) != "MCS_V1")
    {
        throw StrException("格式错误");
    }
    mcsHead.TryLoad(i);
    for(McsFrame* fp : frames)
    {
        delete fp;
    }
    frames.clear();
    for(int k = 0; k < mcsHead.GetFrameCount(); ++k)
    {
        auto fp = new McsFrame();
        fp -> SetMcsHeadPointer(this->GetMcsHead());
        fp -> TryLoad(i);
        frames.append(fp);
    }
    return true;
}

bool Mcs::TryDump(std::ostream &o)
{
    o.write("MCS_V1", 6);
    mcsHead.TryDump(o);
    for(McsFrame* fp : frames)
    {
        fp -> TryDump(o);
    }
    return true;
}

bool Mcs::IsDumpable()
{
    if(!mcsHead.IsDumpable())
        return false;
    for(McsFrame* fp : frames)
        if(!(fp -> IsDumpable()))
            return false;
    return true;
}
